package cn.thornbird.orgsync.controller;

import cn.thornbird.orgsync.annotation.CurrentUser;
import cn.thornbird.orgsync.entity.LoginLog;
import cn.thornbird.orgsync.exception.ForbiddenException;
import cn.thornbird.orgsync.exception.NotFoundException;
import cn.thornbird.orgsync.model.PageList;
import cn.thornbird.orgsync.model.Result;
import cn.thornbird.orgsync.model.UserData;
import cn.thornbird.orgsync.model.combined.LoginLogObject;
import cn.thornbird.orgsync.service.LoginLogService;
import cn.thornbird.orgsync.vo.request.LoginLogQueryRequest;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

@RestController
@RequestMapping(value = "/api/loginLog")
@Api
@AllArgsConstructor
public class LoginLogController {

    private final LoginLogService loginLogService;

    @GetMapping("find/{id}")
    public Result<LoginLogObject> find(@CurrentUser @ApiIgnore UserData userData, @PathVariable String id)
            throws NotFoundException, ForbiddenException {
        userData.checkAuthority(UserData.ADMIN);
        return Result.success(loginLogService.find(id));
    }

    @GetMapping("query")
    public Result<PageList<LoginLog>> query(@CurrentUser @ApiIgnore UserData userData,
                                            LoginLogQueryRequest loginLogQueryRequest,
                                            Integer pageNumber, Integer pageSize) throws ForbiddenException {
        userData.checkAuthority(UserData.ADMIN);
        return Result.success(loginLogService.query(loginLogQueryRequest, pageNumber, pageSize));
    }

}
